package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository repository;
    @Autowired
    private MongoTemplate template;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private TravelCommentRepository travelCommentRepository;


    @Override
    public void save(Travel travel) {
        // 新增操作需要把对象id设置为null,否则可能会出现新增不会将自动生成的id注入
        travel.setId(null);
        repository.save(travel);
    }

    @Override
    public void update(Travel travel) {
        repository.save(travel);
    }

    @Override
    public List<Travel> list() {
        return repository.findAll();
    }

    @Override
    public Travel get(String id) {

        // 页面需要使用到作者头像,所以需要查询到作者对象存入到对应的游记中
        Optional<Travel> op = repository.findById(id);
        if (op.isPresent()) {
            Travel travel = op.get();
            UserInfo userInfo = userInfoService.get(travel.getUserId());
            travel.setAuthor(userInfo);
            Destination destination = destinationService.get(travel.getDestId());
            travel.setDest(destination);
            return travel;
        }
        return null;
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        // 查询对象
        Query query = new Query();



        // 封装分页查询条件
        // 目的地下的游记
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (qo.getDayType() != -1){
            Map<String, Integer> day = qo.getDay();
            query.addCriteria(Criteria.where("day").gte(day.get("min")).lte(day.get("max")));
        }
        // 查总数
        long count = template.count(query, Travel.class);
        if (count == 0){    // 如果总条数为0,表示没有数据,返回一个空页面
            return Page.empty();
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<Travel> travels = template.find(query, Travel.class);

        Page<Travel> page = new PageImpl<>(travels, pageable, count);

        for (Travel travel : page) {
            UserInfo user = userInfoService.get(travel.getUserId());
            travel.setAuthor(user);
        }
        return page;
    }

    @Override
    public void changeState(String id, int state) {

        Travel travel = this.get(id);
        if (travel.getState() != state ) {
            if (state == Travel.STATE_RELEASE) {// 审核通过
                // 该变游记状态
                travel.setState(state);
                // 修改发布时间(releaseTime)
                travel.setReleaseTime(new Date());
                // 修改最后更新时间(lastUpdateTime)
                travel.setLastUpdateTime(new Date());
                // 记录 (审核人员,审核时间...)
            }else { // 审核拒绝
                travel.setState(state);
            }
            this.update(travel);
        }
    }

    @Override
    public String getContentById(String id) {
        Travel travel = this.get(id);
        return travel.getContent();
    }

    @Override
    public void saveOrUpdate(Travel travel) {

        //目的地名称
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());

        // 创建时间
        travel.setCreateTime(new Date());

        // 发布时间

        // 最新更新时间
        travel.setLastUpdateTime(new Date());

        if (StringUtils.hasLength(travel.getId())) {   // 添加操作
            // 点击量,回复数,收藏数,分享数,点赞数
            this.update(travel);
        }else{
            this.save(travel);
        }
    }

    @Override
    public List<Travel> queryTravels(String destId) {

        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Travel> list = repository.findByDestId(destId, pageable);
        return list;
    }

    @Override
    public List<TravelComment> comments(String travelId) {


        return  travelCommentRepository.findByTravelId(travelId);
    }
}
